package com.qianfeng.project;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.hariro.adapter.ViewPager_Adapter;
import com.hariro.fragment.My_Fragment;
import com.hariro.fragment.New_Fragment;
import com.hariro.fragment.Search_Fragment;
import com.hariro.fragment.Shake_Fragment;
import com.hariro.fragment.ShiYi_Fragment;

public class MainActivity extends FragmentActivity implements
		OnCheckedChangeListener, OnPageChangeListener {

	private long firstExitTime;
	private ViewPager viewPager_main;
	private RadioGroup radioGroup_main;
	private RadioButton radioButton_new = null;
	private List<Fragment> totalList = new ArrayList<Fragment>();
	private Shake_Fragment shake_Fragment = new Shake_Fragment();
	private int position = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// 初始化UI组件;
		initView();

		// 添加碎片;
		addFragment();

	}

	private void addFragment() {
		// 1.添加"新款"碎片
		New_Fragment new_Fragment = new New_Fragment();
		totalList.add(new_Fragment);
		// 2.添加"搜索"碎片
		Search_Fragment search_Fragment = new Search_Fragment();
		totalList.add(search_Fragment);
		// 3.添加"试衣"碎片
		ShiYi_Fragment shiyi_Fragment = new ShiYi_Fragment();
		totalList.add(shiyi_Fragment);
		// 4.添加"摇一摇"碎片
		Shake_Fragment shake_Fragment = new Shake_Fragment();
		totalList.add(shake_Fragment);
		// 5. 添加"我的"碎片
		My_Fragment my_Fragment = new My_Fragment();
		totalList.add(my_Fragment);

		radioGroup_main.setOnCheckedChangeListener(this);
		// 给主页面的ViewPager设置适配器;
		// viewPager_main.setOffscreenPageLimit(0);
		viewPager_main.setAdapter(new ViewPager_Adapter(this
				.getSupportFragmentManager(), totalList));
		// 给ViewPager添加页面改变监听器;
		viewPager_main.setOnPageChangeListener(this);

	}

	private void initView() {
		// TODO Auto-generated method stub
		radioGroup_main = (RadioGroup) findViewById(R.id.radioGroup_Tab);
		viewPager_main = (ViewPager) findViewById(R.id.viewPager_main);
		radioButton_new = (RadioButton) findViewById(R.id.radioButton_new);
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		switch (checkedId) {
		case R.id.radioButton_new:
			position = 0;
			break;
		case R.id.radioButton_search:
			position = 1;
			break;
		case R.id.radioButton_shiyi:
			position = 2;
			break;
		case R.id.radioButton_shake:
			position = 3;
			break;
		case R.id.radioButton_my:
			position = 4;
			break;
		}
		viewPager_main.setCurrentItem(position);

	}

	@Override
	public void onPageScrollStateChanged(int position) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODORadioButton Auto-generated method stub
	}

	@Override
	public void onPageSelected(int position) {
		radioButton_new.setChecked(false);
		RadioButton currentRadioButton = (RadioButton) radioGroup_main
				.getChildAt(position);
		currentRadioButton.setChecked(true);
		radioButton_new = currentRadioButton;
		// 如果不是在"摇一摇"的界面,就让"摇一摇"的功能失效;
		// if (position != 3) {
		// shake_Fragment.unRegisterListener();
		// }
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			if (System.currentTimeMillis() - firstExitTime > 2000) {
				firstExitTime = System.currentTimeMillis();
				Toast.makeText(this, "再按一次退出程序！", Toast.LENGTH_SHORT).show();
			} else {
				finish();
			}
			return false;
		}
		// 这句话是默认的按了返回键，如果栈里没有activity就会返回到Home
		return super.onKeyDown(keyCode, event);
	}
}
